﻿/*
*┌─────────────────────────────────┐
*│　描   述：用于开始场景和判断AB包
*│　类   名：StartScene.cs 
*│　创  建  人：DD
*│　创建时间：9.1
*│　Copyright(C) 2020 by DD
*└─────────────────────────────────┘
*┌─────────────────────────────────┐
*│　修  改  人：
*│　创建日期：
*└─────────────────────────────────┘
*/

using AppsFlyerSDK;

using DG.Tweening;

using System.Collections;

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace DD
{
    public class StartScene : MonoBehaviour,IAppsFlyerConversionData
    {

        #region AF回调

        public void onConversionDataFail(string error)
        {
            Debug.Log("onConversionDataFail     " + error);
        }

        public void onAppOpenAttribution(string attributionData)
        {
            Debug.Log("onAppOpenAttribution     " + attributionData);
        }

        public void onAppOpenAttributionFailure(string error)
        {
            Debug.Log("onAppOpenAttributionFailure     " + error);
        }

        public void onConversionDataSuccess(string conversionData)
        {
            Debug.LogError("开始判断AB包");
            Debug.Log ( "<color=red>" + conversionData + "</color>" );
            if (conversionData.ToString().Contains(isAPackage))
            {
                ABPackage.Instance.isA = 1;
                Event.Instance.Track ( "A" );
            }
            else
            {
                ABPackage.Instance.isA = 2;
                Event.Instance.Track ( "B" );
            }

            //Comp 在这里测试B包
            //ABPackage.Instance.isA = 2;
            PlayerPrefs.SetInt ( "AorB" , ABPackage.Instance.isA );

            _isChangeScene = true;
            Debug.Log(string.Format("判断,{0}包", ABPackage.Instance.isA == 2 ? "B" : "A"));
        }
        #endregion

        [SerializeField]
        private Slider _slider;
        [SerializeField]
        private Text _text;

        AsyncOperation _async;


        bool isLoading;                         //是否可以加载
        [SerializeField]
        bool _isChangeScene;               //是否可以切换场景

        public string isAPackage = "\"Organic\"";

        float _tempVelocity;
        float tempValue = 0;

        int LoadCount=0;


        private void Start()
        {

            LoadCount = 0;

            isLoading = true;
            _isChangeScene = false;
            //判断AB

            if ( ABPackage.Instance.isA == 0 )
            {
                if ( Application.internetReachability == NetworkReachability.NotReachable )
                {
                    Debug.Log ( "没有联网" );
                    PlayerPrefs.SetInt ( "AorB" , 0 );
                    _isChangeScene = true;
                }
                else
                {
                    Debug.Log ( "联网，开始回调" );
                    AppsFlyer.getConversionData ( this.gameObject.name );
                }
            }
            else
            {
                _isChangeScene = true;
                Debug.Log ( "已经判断过" );
            }

            _async = SceneManager.LoadSceneAsync(1);
            _async.allowSceneActivation = false;
        }
        private void Update()
        {
            if (_slider && isLoading)
            {
                tempValue = Mathf.Lerp(tempValue, _async.progress / 9 * 10, Time.deltaTime * 2);
                _text.text = (int)(tempValue * 100) + "%";
                _slider.value = tempValue;
                if (tempValue >= 0.995)
                {
                    _slider.value = 1;
                    _text.text = "Loading...";
                    isLoading = false;
                    _text.GetComponent<UIAlphaChange>().enabled = true;
                    Debug.Log(this.GetType() + "———等待三秒加载场景并判断AB包———");
                    Debug.Log(this.GetType() + "———若非联网直接进入，若联网没回调出来则继续等待———");
                    StartCoroutine(LoadScene());
                }
            }

        }
        IEnumerator LoadScene()
        {
            LoadCount++;
            if ( LoadCount >= 6 )
            {
                _isChangeScene = true;
            }
            yield return new WaitForSeconds(2f);
            if (_isChangeScene) {
                _async.allowSceneActivation = true;
            }
            else
            {
                StartCoroutine(LoadScene());
            }
        }

    }
}